import { EnumBetResult, EnumBjlResult } from "@/models/enums/EnumBjlResult";
import { ExcutePlan, RecmInfo, RoundInfo } from "../ExecutePlan";
import { IMoneyStrategy } from "../IMoneyStrategy";
import { IPlan } from "../IExecutePlan";
import { FixedStrategy } from "../recmStrategy/FixedStrategy";
import { BossRabbitMoneyStrategy } from "./BossRabitMoneyStrategy";
import { NormalStepMoneyStrategy } from "./NormalStepMoneyStrategy";
import { PlanAV1StepStrategy } from "./WisdomMoneyStrategyV38Group";


export class WisdomMoneyStrategyV20Group implements IMoneyStrategy {
    subPlan: ExcutePlan[];
    debugCompoenent: string = "WisdomMoneyStrategyV20GroupDebug";
    strategyName: string;
    startIndex: number = -1;
    startLevel: number = 10000;
    planAResetMoney: number[] = [];
    planA1ResetMoney: number[] = [];
    protected _subPlanSum: number = 0;
    public get subPlanSum() {
        return this._subPlanSum;
    }
    constructor(name?: string) {
        this.strategyName = name ?? "GroupMoneyStrategy" + (new Date()).getMilliseconds() + parseInt((Math.random() * 1000).toString());
        this.subPlan = [];
        this.resetStrategy();
    }
    showMoney(roundInfo?: RoundInfo): [EnumBjlResult, number] {
        let zm = 0;
        let xm = 0;
        let recmV = EnumBjlResult.U;
        let recmMoney = 0;

        this.subPlan.forEach(pl => {
            let plRoundInfo = Object.assign({}, roundInfo);
            //plRoundInfo.reverse = this.reverse;
            let [rv, rm] = pl.showMoney(plRoundInfo);
            pl.recmInfo.recmV = rv;
            pl.recmInfo.recmMoney = rm;

            if (pl.recmInfo.recmV == EnumBjlResult.Z) {
                zm += (pl.recmInfo.recmMoney * pl.multiple);

            }
            else if (pl.recmInfo.recmV == EnumBjlResult.X) {
                xm += (pl.recmInfo.recmMoney * pl.multiple);
            }
        });
        if (zm > xm) {
            recmV = EnumBjlResult.Z;
            recmMoney = zm - xm;
        }
        else if (xm > zm) {
            recmV = EnumBjlResult.X;
            recmMoney = xm - zm;
        }
        else {
            recmV = EnumBjlResult.U;
        }

        return [recmV, recmMoney];
    }
    resetRecm() {
        //this.subPlan.forEach(item => item.resetRecm());
        this.subPlan[0].moneyStrategy = [new PlanAV1StepStrategy("A计划", {
            baseMoney: 10,
            stopLFailCount: 3,
            failStepCount: 1,
            winStepCount: -1,
            stepMoney: 10
        })];
        this.subPlan[0].toResult(new RoundInfo());

        this.subPlan[1].moneyStrategy = [new PlanAV1StepStrategy("A1计划", {
            baseMoney: 10,
            stopLFailCount: 3,
            failStepCount: 1,
            winStepCount: -1,
            stepMoney: 10,
        })];
        this.subPlan[1].toResult(new RoundInfo());

        this.subPlan[2].moneyStrategy = [new NormalStepMoneyStrategy("B计划", {
            startMoney: 10,
            addStep: 10,
            devideStep: -10,
            reverse: false,
            resetLevel: 99999999,
            baseMoney: 10,
            betArray: []
        }, true)];
        this.subPlan[2].toResult(new RoundInfo());

        this.subPlan[3].moneyStrategy = [new NormalStepMoneyStrategy("B1计划", {
            startMoney: 10,
            addStep: 10,
            devideStep: -10,
            reverse: false,
            resetLevel: 99999999,
            baseMoney: 10,
            betArray: []
        }, true)];
        this.subPlan[3].toResult(new RoundInfo());

        this.subPlan[4].moneyStrategy = [new BossRabbitMoneyStrategy("C计划", {
            baseMoney: 10,
            winStep: 1,
            failStep: 0,
            prevMoney: 0,
            currentMoney: 10
        })];
        this.subPlan[4].toResult(new RoundInfo());

        this.subPlan[5].moneyStrategy = [new NormalStepMoneyStrategy("D计划", {
            startMoney: 10,
            addStep: 10,
            devideStep: -10,
            reverse: false,
            resetLevel: 99999999,
            baseMoney: 10,
            betArray: []
        }, true)];
        this.subPlan[5].toResult(new RoundInfo());

        this.subPlan[6].moneyStrategy = [new NormalStepMoneyStrategy("D1计划", {
            startMoney: 10,
            addStep: 10,
            devideStep: -10,
            reverse: false,
            resetLevel: 99999999,
            baseMoney: 10,
            betArray: []
        }, true)];
        this.subPlan[6].toResult(new RoundInfo());

        this.subPlan.forEach(plan => {
            plan.recmInfo.summaryInfo.resetSummaryInfo();
            plan.recmInfo.roundSummaryInfo.resetSummaryInfo();
            plan.userRecmInfo.summaryInfo.resetSummaryInfo();
            plan.userRecmInfo.roundSummaryInfo.resetSummaryInfo();
            plan.resetMoney = 0;
        })
    }

    resetStrategy(): void {

        let planA = new ExcutePlan();
        planA.multiple = 0;
        planA.originMultiple = 4;
        planA.planName = "A计划";
        planA.baseMoney = 10;
        planA.recmStrategy = [new FixedStrategy({ reverse: false })];
        planA.moneyStrategy = [new PlanAV1StepStrategy("A计划", {
            baseMoney: 10,
            stopLFailCount: 3,
            failStepCount: 1,
            winStepCount: -1,
            stepMoney: 10,
        })];
        planA.toResult(new RoundInfo());

        let planA1 = new ExcutePlan();
        planA1.multiple = 0;
        planA1.originMultiple = 4;
        planA1.planName = "A1计划";
        planA1.baseMoney = 10;
        planA1.recmStrategy = [new FixedStrategy({ reverse: true })];
        planA1.moneyStrategy = [new PlanAV1StepStrategy("A1计划", {
            baseMoney: 10,
            stopLFailCount: 3,
            failStepCount: 1,
            winStepCount: -1,
            stepMoney: 10,
        })];
        planA1.toResult(new RoundInfo());

        let planB = new ExcutePlan();
        planB.multiple = 1;
        planB.planName = "B计划";
        planB.baseMoney = 10;
        planB.recmStrategy = [new FixedStrategy({ reverse: false })];
        planB.moneyStrategy = [new NormalStepMoneyStrategy("B计划", {
            startMoney: 100,
            addStep: 10,
            devideStep: -10,
            reverse: false,
            resetLevel: 99999999,
            baseMoney: 10,
            betArray: []
        }, true)];
        planB.toResult(new RoundInfo());

        let planB1 = new ExcutePlan();
        planB1.multiple = 1;
        planB1.planName = "B1计划";
        planB1.baseMoney = 10;
        planB1.recmStrategy = [new FixedStrategy({ reverse: true })];
        planB1.moneyStrategy = [new NormalStepMoneyStrategy("B1计划", {
            startMoney: 100,
            addStep: 10,
            devideStep: -10,
            reverse: false,
            resetLevel: 99999999,
            baseMoney: 10,
            betArray: []
        }, true)];
        planB1.toResult(new RoundInfo());

        let planC = new ExcutePlan();
        planC.multiple = 0;
        planC.planName = "C计划";
        planC.baseMoney = 10;
        planC.recmStrategy = [new FixedStrategy({ reverse: false })];
        planC.moneyStrategy = [new BossRabbitMoneyStrategy("C计划", {
            baseMoney: 10,
            winStep: 1,
            failStep: 0,
            prevMoney: 0,
            currentMoney: 10
        })];
        planC.toResult(new RoundInfo());


        let planD = new ExcutePlan();
        planD.multiple = 0;
        planD.planName = "D计划";
        planD.baseMoney = 10;
        planD.recmStrategy = [new FixedStrategy({ reverse: false })];
        planD.moneyStrategy = [new NormalStepMoneyStrategy("planD", {
            startMoney: 10,
            addStep: 10,
            devideStep: -10,
            reverse: false,
            resetLevel: 99999999,
            baseMoney: 10,
            betArray: []
        }, true)];
        planD.toResult(new RoundInfo());

        let planD1 = new ExcutePlan();
        planD1.multiple = 0;
        planD1.planName = "D1计划";
        planD1.baseMoney = 10;
        planD1.recmStrategy = [new FixedStrategy({ reverse: true })];
        planD1.moneyStrategy = [new NormalStepMoneyStrategy("D1计划", {
            startMoney: 10,
            addStep: 10,
            devideStep: -10,
            reverse: false,
            resetLevel: 99999999,
            baseMoney: 10,
            betArray: []
        }, true)];
        planD1.toResult(new RoundInfo());

        this.subPlan.push(planA);
        this.subPlan.push(planA1);
        this.subPlan.push(planB);
        this.subPlan.push(planB1);
        this.subPlan.push(planC);
        this.subPlan.push(planD);
        this.subPlan.push(planD1);
    }
    setSubResult(pl: ExcutePlan, roundInfo: RoundInfo) {
        pl.setResult(roundInfo, pl.recmInfo, pl.multiple, pl.waterRate, pl.fuliRate);

        let m = pl.recmInfo.summaryInfo.winMoney - pl.recmInfo.summaryInfo.failMoney;
        this._subPlanSum += m;

        let userRecmV = pl.toResult(roundInfo);
        let newRoundInfo: RoundInfo = Object.assign({}, roundInfo, { userRecm: userRecmV });
        pl.userRecmInfo.recmV = userRecmV;
        return newRoundInfo;
    }
    toSubMoney(pl: ExcutePlan, roundInfo: RoundInfo, recmInfo: RecmInfo) {
        if (pl.running) {
            let winMoney = pl.recmInfo.roundSummaryInfo.winMoney - pl.recmInfo.roundSummaryInfo.failMoney;

            if (pl.planName == "C计划" || pl.planName == "C1计划") {
                let prevResult = EnumBetResult.NotBet;
                if (recmInfo.summaryInfo.recmResults.length > 0) {
                    prevResult = recmInfo.summaryInfo.recmResults[recmInfo.summaryInfo.recmResults.length - 1];
                }

                if (prevResult == EnumBetResult.NotBet) {
                    roundInfo.userRecm = EnumBjlResult.U;
                }
                else if (prevResult == EnumBetResult.Fail && roundInfo.userRecm != EnumBjlResult.U) {
                    roundInfo.userRecm = roundInfo.userRecm == EnumBjlResult.Z ? EnumBjlResult.X : EnumBjlResult.Z;
                }
            }
            let [rv, rm] = pl.toMoney(roundInfo, pl.recmInfo, pl.userRecmInfo, pl.baseMoney);
            pl.recmInfo.preBetCount = pl.recmInfo.betCount;
            pl.recmInfo.recmV = rv;
            pl.recmInfo.recmMoney = rm;
            if (rv != EnumBjlResult.U) {
                //pl.recmInfo.
                pl.recmInfo.betCount++;
            }

            if ((pl.planName == "B计划" || pl.planName == "B1计划"
                || pl.planName == "C计划" || pl.planName == "C1计划") && pl.multiple > 0
            ) {
                var moneyStrategy = pl.moneyStrategy[0] as NormalStepMoneyStrategy;
                if (moneyStrategy instanceof NormalStepMoneyStrategy) {
                    if (pl.recmInfo.recmMoney >= 100) {
                        moneyStrategy.option.devideStep = -20;
                    }
                    else {
                        moneyStrategy.option.devideStep = -10;
                    }
                }

                // if (pl.recmInfo.recmMoney > pl.baseMoney && !pl.bflag) {
                //     pl.bflag = true;
                // }
                // else if (pl.recmInfo.recmMoney == pl.baseMoney && pl.bflag) {
                //     let multipleLimitMoney = ((1 + pl.multiple) * pl.multiple / 2) * 3000;
                //     if (winMoney > 0) {
                //         pl.bflag = false;
                //         moneyStrategy.resetStrategy();
                //         pl.recmInfo.roundSummaryInfo.resetSummaryInfo();
                //         pl.recmInfo.summaryInfo.resetSummaryInfo();
                //         pl.recmInfo.summaryInfo.resetSummaryInfo();
                //         pl.resetMoney = (pl.resetMoney ?? 0) + winMoney;
                //         if (pl.planName == "C计划" || pl.planName == "C1计划") {
                //             pl.recmInfo = new RecmInfo();
                //         }
                //     }
                //     else if (winMoney < -multipleLimitMoney) {
                //         pl.multiple++;
                //     }
                // }
                // if (winMoney > 0) {
                //     pl.multiple = 1;
                // }
            }
        }
        else {
            pl.recmInfo.recmV = EnumBjlResult.U;
            pl.recmInfo.recmMoney = 0;
        }
        if ((pl.planName == "B计划" || pl.planName == "B1计划"
            || pl.planName == "C计划" || pl.planName == "C1计划") && !pl.bflag && pl.recmInfo.recmMoney > 10) {
            pl.bflag = true;
        }
    }
    toMoney(roundInfo: RoundInfo, recmInfo: RecmInfo, userRecm: RecmInfo, baseMoney: number): [EnumBjlResult, number] {
        let recmV = EnumBjlResult.U;
        let recmMoney = 0;
        this.subPlan.forEach(pl => {

            let plRoundInfo = this.setSubResult(pl, roundInfo);
            let resetMoneyArr: number[] = [];
            if (pl.planName == "A计划") {
                resetMoneyArr = this.planAResetMoney;
            }
            else if (pl.planName == "A1计划") {
                resetMoneyArr = this.planA1ResetMoney;
            }
            if (resetMoneyArr.length > 0) {
                let resetMoney = resetMoneyArr[resetMoneyArr.length - 1];
                let winMoney = pl.recmInfo.roundSummaryInfo.winMoney - pl.recmInfo.roundSummaryInfo.failMoney;
                if (winMoney > resetMoney) {
                    resetMoneyArr.pop();

                    pl.resetRecm();
                    pl.recmInfo.roundSummaryInfo.resetSummaryInfo();
                    pl.multiple = pl.originMultiple + (resetMoneyArr.length)

                }
            }

            this.toSubMoney(pl, plRoundInfo, userRecm);

            let rv = pl.recmInfo.recmV;
            let rm = pl.recmInfo.recmMoney;
            let wf = pl.recmInfo.roundSummaryInfo.failMoney - pl.recmInfo.roundSummaryInfo.winMoney;
            if ((pl.planName == "A计划" || pl.planName == 'A1计划') && rm >= 100 && wf > 0) {
                resetMoneyArr.push(wf);
                pl.resetRecm();
                pl.recmInfo.roundSummaryInfo.resetSummaryInfo();
                rm = pl.baseMoney;
                pl.multiple = pl.originMultiple + (resetMoneyArr.length)
            }

            pl.recmInfo.recmMoney = rm;
            pl.recmInfo.recmV = rv;
        })

        let planAWf = this.subPlan[0].recmInfo.roundSummaryInfo.winMoney - this.subPlan[0].recmInfo.roundSummaryInfo.failMoney;
        let planA1Wf = this.subPlan[1].recmInfo.roundSummaryInfo.winMoney - this.subPlan[1].recmInfo.roundSummaryInfo.failMoney;
        if ((planAWf + planA1Wf) > 200) {
            // this.subPlan[0].recmInfo.recmMoney = this.subPlan[0].baseMoney;
            // this.subPlan[1].recmInfo.recmMoney = this.subPlan[1].baseMoney;
            
            // this.subPlan[0].recmInfo.roundSummaryInfo.resetSummaryInfo();
            // this.subPlan[1].recmInfo.roundSummaryInfo.resetSummaryInfo();
            this.subPlan[0].resetPlan();
            this.subPlan[1].resetPlan();

        }

        let zm = 0;
        let xm = 0;
        this.subPlan.forEach(pl => {
            if (pl.recmInfo.recmV == EnumBjlResult.Z) {
                zm += (pl.recmInfo.recmMoney * pl.multiple);

            }
            else if (pl.recmInfo.recmV == EnumBjlResult.X) {
                xm += (pl.recmInfo.recmMoney * pl.multiple);
            }
        });

        if (zm > xm) {
            recmV = EnumBjlResult.Z;
            recmMoney = zm - xm;
        }
        else if (xm > zm) {
            recmV = EnumBjlResult.X;
            recmMoney = xm - zm;
        }
        else {
            recmV = EnumBjlResult.U;
        }

        return [recmV, recmMoney];
    }

    copyValue(valueObj: any): void {
        var obj = valueObj as WisdomMoneyStrategyV20Group;
        if (obj) {
            this.debugCompoenent = obj.debugCompoenent;
            this.startIndex = obj.startIndex;
            this._subPlanSum = obj._subPlanSum;
            this.planA1ResetMoney = obj.planA1ResetMoney;
            this.planAResetMoney = obj.planAResetMoney;
            for (let i = 0; i < this.subPlan.length; i++) {
                this.subPlan[i].copyValue(obj.subPlan[i]);
            }
        }
    }
}